﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcs.Web.Admin.spaceinfo
{
    public partial class adjust : Web.Admin.BasePage
    {
        lcs.BLL.spaceinfo bll = new lcs.BLL.spaceinfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                //BindData();
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataout();

        }
        protected void btnSearchin_Click(object sender, EventArgs e)
        {
            BindDatain();

        }
        public void BindDataout()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and spname like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtstore.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and sname like '%{0}%'", txtstore.Text.Trim());
            }
            if (Session["login"] != null)
            {
                DataSet u = (DataSet)Session["login"];

                if (u.Tables[0].Rows[0]["roles_id"].ToString() == "2")
                {
                    strWhere.AppendFormat(" and uid={0}", u.Tables[0].Rows[0]["user_id"]);
                }
            }
            ds = bll.GetListradionotempty(strWhere.ToString());
            rblout.DataSource = ds.Tables[0];
            rblout.DataTextField = "spname";
            rblout.DataValueField = "spid";
            rblout.DataBind();
        }
        public void BindDatain()
        {
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {

                strWhere.AppendFormat(" and spname like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtinstore.Text.Trim() != "")
            {

                strWhere.AppendFormat(" and sname like '%{0}%'", txtinstore.Text.Trim());
            }
            if (Session["login"] != null)
            {
                DataSet u = (DataSet)Session["login"];

                if (u.Tables[0].Rows[0]["roles_id"].ToString() == "2")
                {
                    strWhere.AppendFormat(" and uid={0}", u.Tables[0].Rows[0]["user_id"]);
                }
            }
            ds = bll.GetListradio(strWhere.ToString());
            rblin.DataSource = ds.Tables[0];
            rblin.DataTextField = "spname";
            rblin.DataValueField = "spid";
            rblin.DataBind();
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (cblmaterial.Visible == false)
            {
                int outid = int.Parse(rblout.SelectedValue.ToString());
                int inid = int.Parse(rblin.SelectedValue.ToString());
                lcs.BLL.inventory bi = new BLL.inventory();
                bi.Updateadjust(outid, inid);
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "调整成功！", "../inventory/List.aspx");
            }
            else
            {
                int outid = int.Parse(rblout.SelectedValue.ToString());
                int inid = int.Parse(rblin.SelectedValue.ToString());
                string chkSelect = "";
                for (int i = 0; i < cblmaterial.Items.Count; i++)
                {
                    if (cblmaterial.Items[i].Selected == true)
                        chkSelect += cblmaterial.Items[i].Value + ",";
                }
                if (chkSelect != "")
                    chkSelect = chkSelect.Substring(0, chkSelect.Length - 1);

                lcs.BLL.inventory bi = new BLL.inventory();
                bi.Updateadjustbymid(outid, inid, chkSelect, int.Parse(ddlstatus.SelectedValue.ToString()));
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "调整成功！", "../inventory/List.aspx");

            }
        }

        protected void btnnotall_Click(object sender, EventArgs e)
        {
            if (cblmaterial.Visible == false)
            {
                cblmaterial.Visible = true;
                lcs.BLL.inventory bi = new BLL.inventory();
                if (rblout.SelectedValue != null)
                {
                    int outid = int.Parse(rblout.SelectedValue.ToString());
                    DataSet ds = bi.Getmatralbyspid(outid);
                    cblmaterial.DataSource = ds.Tables[0];
                    cblmaterial.DataTextField = "material";
                    cblmaterial.DataValueField = "mid";
                    cblmaterial.DataBind();
                }
            }
            else
            {
                cblmaterial.Visible = false;
            }
        }

    }
}